<?php
/* @var $this yii\web\View */
/* @var $generator zkc\gii\generators\crud\Generator */

/* @var $model \yii\db\ActiveRecord */
$model          = new $generator->modelClass();
$safeAttributes = $model->safeAttributes();
if (empty($safeAttributes)) {
	$safeAttributes = $model->attributes();
}

$cols = [];

foreach ($generator->getColumnNames() as $attribute) {
	// if (in_array($attribute, $safeAttributes)) {
		$cols[] = [
			'field' => $attribute,
			'title' => $generator->getColumnLabel($attribute),
			'sortable'=>true,
			'width'=>80,
		];
	// }
}

$class = $generator->modelClass;
$pk    = $class::primaryKey();
if (is_array($pk) && count($pk) >= 1) {
	$pk = $pk[0];
}
if (empty($pk)) {
	$pk = 'id';
}

echo "<?php\n";
?>
use yii\web\View;
use yii\helpers\Url;

/* @var $this yii\web\View */

$this->params['body_border'] = false;

$pk =<?= var_export($pk, true); ?>;

$opts = json_encode([
	'pk' => $pk,
	'dataUrl' => Url::to(['index', '_query'=>1]),
	'createUrl' => Url::to(['create']),
	'saveUrl' => Url::to(['save']),
	'viewUrl' => Url::to(['view']),
	'deleteUrl' => Url::to(['delete']),
], JSON_UNESCAPED_UNICODE);

\common\widgets\JsBlock::begin();
<?php
echo "?>\n";
?>
<script type="application/javascript">
	var opts = <?php echo '<?=$opts?>';?>;

	$(function () {

		$('#dg-listview').datagrid({
			singleSelect: true,
			method: 'get',
			rownumbers: true,
			url: opts.dataUrl,
			border: false,
			fit: true,
			striped: true,
			multiSort: true,
			remoteSort: true,
			//toolbar: '#dg-toolbar',
			sortName: "<?=$pk?>",
			sortOrder: "desc",
			footer: '#ft',
			idField: "<?='<?=$pk?>'?>",
			columns: [[
<?php
				foreach ($cols as $col) {
					echo str_repeat("\t", 4) . json_encode($col, JSON_UNESCAPED_UNICODE) . ',' . PHP_EOL;
				} ?>
			]],
			pagination: true,
			toolbar:
			<?php echo "<?php  \backend\widgets\auth\ToolbarButtonFilter::begin() ?>\n" ?>
				[
					{
						id: 'btn-new' // 增加
					},
					{
						id: 'btn-edit' // 修改
					},
					{
						id: 'btn-delete' // 删除
					},
					{
						id: 'btn-view' // 查看
					},
					{
						id: 'btn-import' // 导入
					},
					{
						id: 'btn-export' // 导出
					},
					{
						id: 'btn-audit' // 审核
					},
					{
						id: 'btn-audits' // 批量审核
					}
			]
			<?php echo "<?php  \backend\widgets\auth\ToolbarButtonFilter::end() ?>" ?>,
			loader: function (param, success_callback, error_callback) {

				// 取得查询参数
				var search_params = $('#search-form').serializeObject();
				param = $.extend({}, param, search_params);

				return datagrid_loader({
					url: opts.dataUrl,
					param: param,
					success_callback: success_callback,
					error_callback: error_callback
				});
			}
		});

		$('#btn-new').linkbutton({
			onClick: function () {
				opts.action = 'new';
				opts.row = undefined;
				url = opts.createUrl;
				BX.goto(url);
			}
		});
		$('#btn-edit').linkbutton({
			onClick: function () {
				opts.row = $('#dg-listview').datagrid('getSelected');
				if (opts.row) {
					opts.action = 'edit';
					url = opts.saveUrl;
					url = url.addParam({id: opts.row[opts.pk]});
					BX.goto(url);
				} else {
					$.messager.alert('提示', '请选择要修改的行。', 'alert');
				}
			}
		});
		$('#btn-view').linkbutton({
			onClick: function () {
				opts.row = $('#dg-listview').datagrid('getSelected');
				if (opts.row) {
					opts.action = 'view';
					url = opts.viewUrl;
					url = url.addParam({id: opts.row[opts.pk]});
					BX.goto(url);
				} else {
					$.messager.alert('提示', '请选择要查看的行。', 'warning');
				}
			}
		});
		$('#btn-delete').linkbutton({
			onClick: function () {
				var row = $('#dg-listview').datagrid('getSelected');
				if (row) {
					$.messager.confirm('确认', '确定要删除吗？', function (r) {
						if (r) {
							var url = opts.deleteUrl.addParam({id: row[opts.pk]});
							$.post(url, {}, function (data) {
								data = $.parseJSON(data);
								if (data.type == 'error') {
									$.messager.alert('执行结果', data.message, 'error');
								} else {
									$.messager.alert('执行结果', data.message, 'info');
									$('#dg-listview').datagrid('reload');
								}
							});
						}
					});
				}
			}
		});
// search
		$('#btn-search').linkbutton({
			onClick: function () {
				$('#dg-listview').datagrid('reload');
			}
		});
		$('#btn-clear').linkbutton({
			onClick: function () {
				$('#search-form').form('reset');
			}
		});
	});
</script>
<?php
echo "<?php\n";
?>
\common\widgets\JsBlock::end();
<?php
echo "?>\n";

echo <<<DOC


<div class="easyui-layout" data-options="fit:true,border:false">

	<div class="search-region" data-options="region:'north',height:'auto',split:true,border:true" style="padding-top:5px;padding-left:10px;overflow:hidden">
		<form id="search-form" action="<?= Url::to(['index', '_query' => 1, '_outport' => 1]); ?>">
			<span class="form-field">
				<font color="red">搜索条件示例, 根据实际字段进行修改</font>
			</span>
			<span class="form-field">
				<input type="text" class="easyui-textbox" name="order_no" label="订单号：" labelWidth="60"/>
			</span>
			<span class="form-field">
				<input type="text" class="easyui-textbox" name="account_id" label="账户号：" labelWidth="60"/>
			</span>
			<span class="form-field">
				<input name="sdate" class="easyui-datebox" value="<?=date('Y-m-d');?>" style="width:170px;" label="创建时间：" labelWidth="70"/>
				<input name="edate" class="easyui-datebox" value="<?=date('Y-m-d');?>" style="width:100px;"/>
			</span>
			<span class="form-field">
				<?= \backend\widgets\EnumSelect::widget(['cat' => 'settle.flow_status', 'isInsertOption' => true, 'multiple' => false, 'attributes' => ['name' => 'status', 'label' => '状态：', 'labelWidth'=>"60",]]) ?>
			</span>

			<span class="form-field">
				<a href="javascript:void(0)" id="btn-search" class="easyui-linkbutton" iconCls="icon-search">查询</a>
				<a href="javascript:void(0)" id="btn-clear" class="easyui-linkbutton" iconCls="icon-arrow_undo">清空</a>
				<?php \backend\widgets\auth\ButtonFilter::begin(['route'=>'export'])?>
				<a href="javascript:void(0)" id="btn-export" class="easyui-linkbutton" iconCls="icon-television_out">导出</a>
				<?php \backend\widgets\auth\ButtonFilter::end()?>
			</span>
		</form>
	</div>
	<div data-options="region:'center',border:true">
		<table id="dg-listview">

		</table>
	</div>

</div>
DOC;
?>